package com.example.hello.client.TabLink;

import com.example.hello.BootstrapTabLink;
import com.github.gwtbootstrap.client.ui.TabPane;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.MouseEventDetailsBuilder;
import com.vaadin.client.communication.RpcProxy;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.ui.Connect;

@Connect(BootstrapTabLink.class)
public class BootstrapTabLinkConnector extends AbstractComponentConnector implements ClickHandler {

    private BootstrapTabLinkRpc rpc = RpcProxy.create(BootstrapTabLinkRpc.class, this);

    public BootstrapTabLinkConnector() {
        getWidget().addClickHandler(this);
    }

    @Override
    protected Widget createWidget() {
        return GWT.create(VBootstrapTabLink.class);
    }

    @Override
    public VBootstrapTabLink getWidget() {
        return (VBootstrapTabLink) super.getWidget();
    }

    @Override
    public BootstrapTabLinkState getState() {
        return (BootstrapTabLinkState) super.getState();
    }

    @Override
    public void onStateChanged(StateChangeEvent stateChangeEvent) {
        super.onStateChanged(stateChangeEvent);

        BootstrapTabLinkState state = getState();

        VBootstrapTabLink tabLink = getWidget();

        tabLink.setActive(state.isActive());
        tabLink.setDataTarget(state.getDataTarget());
        tabLink.setText(state.getText());
        tabLink.setCreateTabPane(state.isCreateTabPane());
        tabLink.setTabPane((TabPane) state.getTabPane());
    }

    public void onClick(ClickEvent event) {
        MouseEventDetails details = MouseEventDetailsBuilder.buildMouseEventDetails(
                event.getNativeEvent(), getWidget().getElement());

        rpc.click(details);
    }

}
